package cn.com.jslh.boarpro.main.presenter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import net.idik.lib.slimadapter.SlimAdapter;
import net.idik.lib.slimadapter.SlimAdapterEx;
import net.idik.lib.slimadapter.SlimInjector;
import net.idik.lib.slimadapter.viewinjector.IViewInjector;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.com.jslh.boarpro.R;
import cn.com.jslh.boarpro.main.contract.ChosePersonContract;
import cn.com.jslh.boarpro.main.model.ChosePersonModelImpl;
import cn.com.jslh.boarpro.util.JSONUtil;

/**
 * Created by Administrator on 2017/10/30.
 */

public class ChosePersonPresenterImpl  implements ChosePersonContract.Presenter {

    private ChosePersonContract.View view;
    private ChosePersonContract.Model model;
    private Context context;
    private SlimAdapterEx slimAdapterEx;
    public ChosePersonPresenterImpl(Context context,ChosePersonContract.View view){
        this.context = context;
        this.view = view;
        view.setPresenter(this);
        model= new ChosePersonModelImpl();
    }

    @Override
    public void start() {
        slimAdapterEx.updateData(JSONUtil.JsonArray2List(AddBreedingRecordPresenterImpl.persons)).notifyDataSetChanged();
    }

    @Override
    public void bindRV(RecyclerView recyclerView) {
        slimAdapterEx = SlimAdapter.create(SlimAdapterEx.class)
                .enableDiff()
                .enableEmptyView(context,R.layout.empty_layout)
                .registerDefault(R.layout.chose_person_item, new SlimInjector<JSONObject>() {
                    @Override
                    public void onInject(final JSONObject data, IViewInjector injector) {
                        try {
                            injector.text(R.id.personName,data.getString("FPERSON"))
                                    .clicked(R.id.layout, new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            try {
                                                view.chosePerson(data.getString("FID"),data.getString("FPERSON"));
                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    });
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                })
                .attachTo(recyclerView);
    }

    @Override
    public void search(String s) {
        List<JSONObject> jsonArraynew = new ArrayList<>();
        JSONArray jsonArray = AddBreedingRecordPresenterImpl.persons;
        for (int i = 0; i < jsonArray.length(); i++) {
            try {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String name = jsonObject.getString("FPERSON");
                if(name.contains(s)){
                    jsonArraynew.add(jsonObject);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        slimAdapterEx.updateData(jsonArraynew).notifyDataSetChanged();
    }
}
